const app = getApp();

Page({
  setdata: app.setdata,
  goto: app.goto,
  /**
   * 页面的初始数据
   */
  data: {
    list:[],

    check:[],
    model:0,

    mrRoom:{

    },

    count_price:"0.00",
    order_ids:"",
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.init(this);

    


    this.setData({
      mrRoom: wx.getStorageSync("mrRoom"),
      type: options.type,
    })

    app.api.client_invoice_wyf_sjfp({
      token: wx.getStorageSync("token"),
      idCommunity: wx.getStorageSync("idCommunity"),
      id: this.data.mrRoom.idRoom,
      status: 1,

      interface_error: "1",
    }, (res) => {
      console.log(res)

      for (let i in res.rows){
        res.rows[i].check = -1;
      }

      this.setData({
        list: res.rows,
      })
    
    })




    // client_invoice_wyf_sjfp: Public("/client/invoice/wyf_sjfp"),
    // client_invoice_wyf_sjfp_ycsdf_sjfp: Public("/client/invoice/wyf_sjfp_ycsdf_sjfp"),
    // client_invoice_wyf_sjfp_lssf_sjfp: Public("/client/invoice/wyf_sjfp_lssf_sjfp"),
    //     client_invoice_wyf_sjfp_tcf_sjfp: Public("/client/invoice/wyf_sjfp_tcf_sjfp"),
    //       client_invoice_wyf_sjfp_ycwyf_sjfp: Public("/client/invoice/wyf_sjfp_ycwyf_sjfp"),

    
  },

  setcheck: function (e) {
    let list = this.data.list;
    let check = this.data.check;
    if (list[e.currentTarget.dataset.index].check == -1){
      list[e.currentTarget.dataset.index].check = e.currentTarget.dataset.index;
      check.push(e.currentTarget.dataset.index);
    }else{
      list[e.currentTarget.dataset.index].check = -1;
      check.splice(check.indexOf(e.currentTarget.dataset.index), 1);
    }

    this.setData({
      list:list,
      check: check,
    })

    console.log(e.currentTarget.dataset);

    this.set_count_price();

  },

  selectall: function (e) {
    let list = this.data.list;
    let check = [];
    for (let i in list){
      list[i].check = i;
      check.push(i);
    }

    this.setData({
      list: list,
      check: check
    })

    this.set_count_price();

  },


  set_count_price:function(){
    var check = this.data.check;
    var list = this.data.list;
    var count_price = 0;
    var order_ids = [];

    for (let i in check){
      count_price = count_price + list[check[i]].kpje
      order_ids.push(list[check[i]].id);
    }

    this.setData({
      count_price: count_price.toFixed(2),
      order_ids: order_ids.join(',')
    })

    
    // console.log(count_price)

  },

  selectnoall: function (e) {
    let list = this.data.list;

    for (let i in list) {
      list[i].check = -1;
    }

    this.setData({
      list: list,
      check:[],
    })

    this.set_count_price();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.init(this);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  goto_invoice: function () {

    // ../bill/invoice?type={{}}

    wx.navigateTo({
      url: '../bill/invoice?type='+this.data.type
        // + "type=" + this.data.type
        + "&JSHJ=" + this.data.count_price
        + "&ORDER_ID=" + this.data.order_ids
    })


    

  },



})